package com.codeduck.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codeduck.storage.domain.StorageDO;
import com.codeduck.storage.mapper.StorageMapper;
import com.codeduck.storage.service.IStorageService;
import org.springframework.stereotype.Service;

/**
 * @author: <a href="mailto:codeduck@163.com">Codeduck</a>
 * @createDate: 2022-08-20
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, StorageDO> implements IStorageService {
    @Override
    public StorageDO decreaseStorage(String commodityCode, Integer count) {
        QueryWrapper<StorageDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StorageDO::getCommodityCode, commodityCode);
        StorageDO storageDO = this.baseMapper.selectOne(wrapper);
        storageDO.setCount(storageDO.getCount() - count);
        if (this.baseMapper.updateById(storageDO) > 0) {
            return this.getStorage(commodityCode);
        }
        return null;
    }

    @Override
    public StorageDO getStorage(String commodityCode) {
        return this.getOne(Wrappers.<StorageDO>lambdaQuery().eq(StorageDO::getCommodityCode, commodityCode));
    }
}
